<template>
  <view class="container">
    <view class="form">
      <text class="title">注册</text>
      <input class="input" placeholder="请输入用户名" v-model="username" />
      <input class="input" placeholder="请输入密码" type="password" v-model="password" />
      <input class="input" placeholder="请再次输入密码" type="password" v-model="confirmPassword" />
      <button class="register-btn" @click="handleRegister">注册</button>
      <navigator url="/pages/login/login" class="login-link">已有账号？去登录</navigator>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: '',
    };
  },
  methods: {
    async handleRegister() {
      console.log("执行注册逻辑");
      
      // 1. 表单验证
      if (!this.username || !this.password || !this.confirmPassword) {
        uni.showToast({
          title: '请填写完整信息',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      
      // 2. 验证两次密码是否一致
      if (this.password !== this.confirmPassword) {
        uni.showToast({
          title: '两次密码不一致',
          icon: 'none',
          duration: 2000
        });
        return;
      }
      
      // 3. 调用云函数上传注册信息
      console.log("----------执行添加操作----------");
      try {
        const res = await uniCloud.callFunction({
          name: 'addUser',
          data: {
            username: this.username,
            password: this.password
          }
        });
        
        console.log('注册结果：', res.result);
        
        // 4. 处理返回结果
        if (res.result.code === 0) {
          uni.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 2000
          });
          
          // 延迟跳转到登录页
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/login/login'
            });
          }, 2000);
        } else {
          uni.showToast({
            title: res.result.msg || '注册失败',
            icon: 'none',
            duration: 2000
          });
        }
      } catch (err) {
        console.error('注册异常：', err);
        uni.showToast({
          title: '网络错误，请重试',
          icon: 'none',
          duration: 2000
        });
      }
    }
  }
}
</script>

<style>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}
.form {
  width: 80%;
  background-color: #fff;
  padding: 30rpx;
  border-radius: 10rpx;
  box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.title {
  font-size: 40rpx;
  font-weight: bold;
  margin-bottom: 30rpx;
}
.input {
  width: 100%;
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 6rpx;
  padding: 0 20rpx;
  margin-bottom: 20rpx;
  box-sizing: border-box;
}
.register-btn {
  width: 100%;
  height: 80rpx;
  background-color: #1677ff;
  color: #fff;
  border-radius: 6rpx;
  margin-top: 20rpx;
}
.login-link {
  margin-top: 20rpx;
  font-size: 28rpx;
  color: #1677ff;
}
</style>